# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html

# useful for handling different item types with a single interface
import codecs
import time
from itemadapter import ItemAdapter
import  pymysql
class WeatherPipeline:
    def process_item(self, item, spider):
        conn=pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        passwd='123456',
        db='scrapydb',
        charset='utf8'
        )
        city=item['city']
        tem=item['tem']
        wea=item['wea']
        date= item['date']
        with conn.cursor() as c:
            sql = f"insert into weather(cityname,wea,tem,riqi) values('{city}', '{wea}', '{tem}','{date}')"
            try:
                c.execute(sql)
                conn.commit()
            except:
                # 发生错误时回滚
                conn.rollback()

        conn.close()
        return item
